/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.querie.SI;

import com.querie.Voldemort.VoldemortH;
import com.querie.beans.SessionAll;
import com.querie.beans.ItemAll;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
 *
 * @author Naushin
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


public class PutSessionData {
 
VoldemortH vold=new VoldemortH();
Gson gson=new Gson();

 //public static List<Integer> list10=new ArrayList<Integer>();




 public void putAll(int id, List<Integer>I, List<Integer> C){
List<ItemAll> list=new ArrayList<ItemAll>();
String id2=Integer.toString(id);
//String present=vold.getValue(id2, "Sessions");
//if(present!=null){
//return;
//}
 //else if(present==null){
System.out.println("in put all");
//list10.add(id);
System.out.println("id"+id+"items"+I+"Count"+C);
SessionAll sesAll=new SessionAll();

for ( int k=0;k < I.size();k++){

ItemAll iall=new ItemAll();
iall.setItemId(I.get(k));
System.out.println(I.get(k));
iall.setCount(C.get(k));
System.out.println(C.get(k));


list.add(iall);
    }

sesAll.setItems(list);
sesAll.setSessionId(id);



try{
    String sesAllValue= gson.toJson(sesAll);
    vold.putValue(""+id,sesAllValue, "SessionThree");
}catch (Exception ex){
   System.out.println("Exception: "+ex);
}
finally{

  }
System.out.println("SessionList Added");
System.out.println("Session id generated: "+id);
//return list10;
return;
}
 /*public void putData(){
 putAll();
 }*/
 //else return;
//}
/**
 * put data in stores for a new session
 * @param Session session id of the new session
 * @param item
 */
 public void putData(int Session,String item){
        VoldemortH v=new VoldemortH();
        String s=v.getValue(Integer.toString(Session), "SessionThree");
          SessionAll sessionObj=new SessionAll();
      Gson gson = new Gson();

    sessionObj = gson.fromJson(s,SessionAll.class);
    List<Integer> ls=new ArrayList<Integer>();
    List<Integer> countls=new ArrayList<Integer>();
    for(int k=0; k<sessionObj.getItems().size();k++){

         Integer itemName=(sessionObj.getItems().get(k).getItemId());//each itemName
        Integer count=(sessionObj.getItems().get(k).getCount());//each itemName

         ls.add(itemName);
         countls.add(count);

     }
     ls.add(Session);
     countls.add(1);
     putAll(Session,ls,countls);

 }

 public void checkIfInserted(){
VoldemortH vh= new VoldemortH();

String sesA=vh.getValue("45","SessionThree");
if(sesA==null){
System.out.println("Session not present");

     }
 else System.out.println("Element found"+sesA);
 }
public static void main(String [] args){


    PutSessionData pd = new PutSessionData();
   // pd.putData();
    pd.checkIfInserted();

}
}
